package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.object.pojo.BleBeacon;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.BleBeaconService;
import com.tbit.uqbike.webmanager.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: chen
 * @Description: 道钉
 * @Date: 创建于 10:00 2020/5/15
 */
@RestController
@RequestMapping("/bleBeacon")
public class BleBeaconController {
    @Autowired
    private BleBeaconService bleBeaconService;

    @RequestMapping("/queryPage")
    public JsonResult queryPage(Integer state, Integer parkPointId, String machineNO, Integer accountId,
                                @RequestParam(defaultValue = "1") Integer pageNO, @RequestParam(defaultValue = "10") Integer rowCount) {
        Assert.notNull(parkPointId, "ver.parkPointId.notNull");
        Assert.notNull(accountId, "ver.accountId.notNull");
        List<BleBeacon> list = bleBeaconService.queryPage(state, parkPointId, machineNO, pageNO, rowCount, accountId);
        Integer total = bleBeaconService.queryCount(state, parkPointId, machineNO, accountId);
        return JsonResult.ok(list, total);
    }


    @RequestMapping("/add")
    public JsonResult insert(BleBeacon bleBeacon) {
        Assert.notNull(bleBeacon.getParkPointId(), "ver.parkPointId.notNull");
        Assert.notNull(bleBeacon.getMachineNO(), "ver.machineNO.notNull");
        Assert.notNull(bleBeacon.getAccountId(), "ver.accountId.notNull");
        bleBeaconService.insert(bleBeacon);
        return JsonResult.ok();
    }

    /**
     * 批量添加
     *
     * @param parkPointId 站点id
     * @param machineNOs  设备编号
     * @param accountId   运营区域id
     * @return
     */
    @RequestMapping("/addBatch")
    public JsonResult addBatch(Integer parkPointId, String machineNOs, Integer accountId) {
        Assert.notNull(parkPointId, "ver.parkPointId.notNull");
        Assert.notNull(machineNOs, "ver.machineNO.notNull");
        Assert.notNull(accountId, "ver.accountId.notNull");
        List<String> list = Arrays.asList(machineNOs.split(","));
        List<String> exist = bleBeaconService.addBatch(parkPointId, list, accountId);
        return JsonResult.ok(exist);
    }

    @RequestMapping("/del")
    public JsonResult del(Integer bleBeaconId) {
        Assert.notNull(bleBeaconId, "ver.bleBeaconId.notNull");
        bleBeaconService.del(bleBeaconId);
        return JsonResult.ok();
    }
}
